/*  This file creates inputs for counterfactual analysis      */
clear all
set more off
cd ""   //target directory


clear
use "./prepare/allbasic.dta",clear
gen sa=0
replace sa=1 if sysid==0
gen af=0
replace af=1 if sysid>0
bys hsa year:egen nsa=total(sa)
bys hsa year:egen naf=total(af)

bys hsa year prechs:gen nval=_n==1
replace nval=0 if prechs==1
bys hsa year:replace nval=sum(nval)
bys hsa year:replace nval=nval[_N]


keep if nsa==3 & naf<6 
foreach YEAR in 2006 2007 2008 2009 {
preserve
keep if year==`YEAR'
keep ahaid
tempfile aha`YEAR'
save `aha`YEAR''
restore
}


clear
use `aha2006',clear
merge 1:1 ahaid using `aha2007'
keep if _merge==3
drop _merge
merge 1:1 ahaid using `aha2008'
keep if _merge==3
drop _merge
merge 1:1 ahaid using `aha2009'
keep if _merge==3
drop _merge
tempfile aha
save `aha'


clear 
use "./prepare/allbasic.dta",clear
merge m:1 ahaid using `aha'
keep if _merge==3
drop _merge
gen const=1
bys hsa year vid:egen nh=total(const)
bys sysid year vid:egen nsysmem=total(const)
replace nmemb=0 if sysid==0
replace nsysmem=0 if sysid==0
drop if nsysmem<nmemb
drop if nh<nhosp
//first create syschs_13chs.csv
preserve
keep if sysid>0
outfile ahaid year hsa bdtot prechs adj_vnd nprofit profit teaching ncompet ratio perc_mcr perc_mcd sysid using "./prepare/syschs_13chs.csv",c replace
restore
//create cftf3fmYEAR_13chs.csv
foreach YEAR in 2006 2007 2008 2009 {
preserve
keep if year==`YEAR'
sort hsa sysid bdtot
outfile hsa ahaid sysid bdtot prechs using "./prepare/ctft3fm`YEAR'_13chs",c replace
restore
}







****************************** Export mkt-related variables: HHI, totpop65, mktFEs
******************************
******************************
clear all
set more off
cd ""  //target directory



clear
insheet using choice.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newchoice`i'
}
reshape long newchoice,i(ahaid year) j(vid)
tempfile chs
save `chs'

insheet using vlag.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newvlag`i'
}
reshape long newvlag,i(ahaid year) j(vid)
tempfile vlag
save `vlag'


insheet using exov1mkt.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newv1mkt`i'
}
reshape long newv1mkt,i(ahaid year) j(vid)
tempfile v1mkt
save `v1mkt'



insheet using exov2mkt.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newv2mkt`i'
}
reshape long newv2mkt,i(ahaid year) j(vid)
tempfile v2mkt
save `v2mkt'


insheet using exovmksh.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newvmktsh`i'
}
reshape long newvmktsh,i(ahaid year) j(vid)
tempfile vmktsh
save `vmktsh'


insheet using v1sys.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newv1sys`i'
}
reshape long newv1sys,i(ahaid year) j(vid)
tempfile v1sys
save `v1sys'


insheet using v2sys.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newv2sys`i'
}
reshape long newv2sys,i(ahaid year) j(vid)
tempfile v2sys
save `v2sys'


insheet using vsysh.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newvsysh`i'
}
reshape long newvsysh,i(ahaid year) j(vid)
tempfile vsysh
save `vsysh'



//IV inputs  ### UPDATED on 07/24/2019: Philipp mentioned using an alternative IV, which is the vendor share within the multi-region chain; input for IV is altiv_dom/msh_tmp.csv
clear
insheet using "./prepare/iv_dom_tmp.csv",clear
rename v14 hsayr_id
forvalues i=1/13 {
rename v`i' ivdom`i'
}
reshape long ivdom,i(hsayr_id) j(vid)
tempfile ivdom
save `ivdom'


insheet using "./prepare/iv_msh_tmp.csv",clear
rename v14 hsayr_id
forvalues i=1/13 {
rename v`i' ivmsh`i'
}
reshape long ivmsh,i(hsayr_id) j(vid)
tempfile ivmsh
save `ivmsh'



insheet using exovtipmsh.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newvtipmsh`i'
}
reshape long newvtipmsh,i(ahaid year) j(vid)
merge 1:1 ahaid year vid using `chs'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `vlag'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `v1mkt'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `v2mkt'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `vmktsh'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `v1sys'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `v2sys'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `vsysh'
keep if _merge==3
drop _merge
tempfile vtipmsh
save `vtipmsh'


clear
use "./prepare/allbasic.dta",clear
merge 1:1 ahaid year vid using `vtipmsh'
keep if _merge==3
drop _merge


sort sysid year
egen sysyr_id=group(sysid year)
sort hsa year
egen hsayr_id=group(hsa year)



merge m:1 hsayr_id vid using `ivmsh'
drop _merge

merge m:1 hsayr_id vid using `ivdom'
drop _merge




** bdtot: 25%: 25; 50%: 80; 75%: 193
sum bdtot if sysid==0,detail  
gen bdrng=0

replace bdrng=1 if bdtot<=25
replace bdrng=2 if bdtot>25 & bdtot<=80
replace bdrng=3 if bdtot>80 & bdtot<=193
replace bdrng=4 if bdtot>193
tab bdrng,gen(bdqntl)



foreach i in 1 2 3 4 {
gen v1bd`i'=newv1mkt*bdqntl`i'
gen vmshbd`i'=newvmktsh*bdqntl`i'
gen vlagbd`i'=vlag*bdqntl`i'
}




********* Now incorporate new inputs for market (un)observables
***%%%*** ### UPDATED on 04/09/2020: Demographics: population and income per capita
preserve
clear
use "./prepare/demographic4mkt"
duplicates drop hsa year,force
keep hsa year totpop totpop65 avginc
gen perc65=totpop65/totpop
replace totpop=log(totpop)
replace totpop65=log(totpop65)
replace avginc=log(avginc)
tempfile pop
save `pop'
restore
***%%%***




***%%%*** ### UPDATED on 04/09/2020: create HHI at the market level using total admissions: admtot (constructed internally using the file "for2ndstage")
preserve
keep if vid==13
keep id year admtot hsa
bys hsa year:egen totadm_mkt=total(admtot)
gen admsh=admtot/totadm_mkt
bys hsa year:egen hhi=total(admsh^2)
duplicates drop hsa year,force
keep hsa year hhi
tempfile hhi
save `hhi'
restore

***%%%***



***%%%*** ### UPDATED on 04/15/2020: Use the combined market FEs---mktFEs_comb1 (using adoption ratio as DV)
preserve

clear
input hsa mktfe
1 0
end
tempfile firsthsa
save `firsthsa'


clear
import delim using "./prepare/mktFEs_comb1.csv",delimiters(",") varnames(1)
rename (v1 comb) (hsa mktfe)
drop if strpos(hsa,"_Ihsa_")==0
replace hsa=subinstr(hsa,"_Ihsa_","",1)
destring,replace force
append using `firsthsa',force

gen below25cat=0
gen below50cat=0
gen below75cat=0
gen below100cat=0

sum mktfe,detail

replace below25cat=1 if mktfe<=r(p25)
replace below50cat=1 if mktfe<=r(p50)&mktfe>r(p25)
replace below75cat=1 if mktfe<=r(p75)&mktfe>r(p50)
replace below100cat=1 if mktfe>r(p75)

tempfile mktfe_comb
save `mktfe_comb'

restore


***%%%***



** MErge stuff

merge m:1 hsa using `mktfe_comb'
keep if _merge==3
drop _merge


merge m:1 hsa year using `pop'
keep if _merge==3
drop _merge



merge m:1 hsa year using `hhi'
keep if _merge==3
drop _merge


*** Export mkt variables: HHI, pop65, and mktFEs
duplicates drop hsa year,force
sort hsa year
export delim hsa year hhi totpop65 below50cat below75cat below100cat using "./prepare/mktshift.csv",delim(",") novar replace


